import { getCanBuyDays, getCalendartickt, login } from './src/thirdServices/palaceApi';
import PalaceHelper from './src/middleware/palaceHelper';
import dayjs from 'dayjs';

const getPalaceTicketPool = async (appointDate) => {
  let ticketPool;
  const res = await getCanBuyDays();
  if (res.data.code === 200) {
    const canJson = JSON.parse(res.data.data);
    const date = new Date(canJson.time);
    const dateList: string[] = [];
    for (let i = 0;i< canJson.days; i++) {
        let day = date.getDate();
        date.setDate(day + 1);
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const monthStr = month < 10 ? `0${month}` : `${month}`
        const d = date.getDate();
        const dStr = d < 10  ? `0${d}` : `${d}`;
        dateList.push(`${year}-${monthStr}-${dStr}`);
    }
    
    if (dateList.includes(appointDate)) {
      const date = new Date(appointDate);
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const monthStr = month < 10 ? `0${month}` : `${month}`
      const params = {
        parkId: 11324,
        year,
        month: monthStr,
        merchantId: 2655,
        merchantInfoId: 2655
      };

      const calendarRes = await getCalendartickt(params);

      if (calendarRes.data.status === 200) {
        const calendarList = calendarRes.data.data;
        const vo = calendarList.find(item => item.occDate === appointDate);
        if (vo.saleStatus === 'T') {
          if (vo.parkFsyyDetailDTOS) {
            const dto = vo.parkFsyyDetailDTOS.find(dto => !!dto.stockNum);
            if (dto.stockNum) {
              ticketPool = vo;
              ticketPool.DTO = dto;
            }
          }
        } else {
          console.log('闭馆');
        }
      }
    }
  }
  return ticketPool;
}

const createPalaceTicket = async (poolVO, touristList) => {
  const palaceHelper = new PalaceHelper();
  const isBuy = await palaceHelper.buy(poolVO, touristList);
  return isBuy;
};

const buyPalaceTicket = async (appointDate, touristList) => {
  const ticketPool = await getPalaceTicketPool(appointDate);
  let isBuy = false;
  if (ticketPool) {
    try {
      isBuy = await createPalaceTicket(ticketPool, touristList);
    } catch (error) {
      
    }
  }
  return isBuy;
}


const main = async () => {
    await buyPalaceTicket('2024-04-03', [{
      ticketType: '标准票',
      name: '郑昌军',
      idType: '0',
      idNo: '230183199003254410'
    }]);
};


// const getTicketDate = async () => {
//   const res = await getCanBuyDays();
//   if (res.data.code === 200) {
//     const canJson = JSON.parse(res.data.data);
//     const date = new Date(canJson.time);
//     const endDate = new Date(canJson.time);
//     endDate.setDate(date.getDate() + canJson.days);
//     const params = {
//       parkId: 11324,
//       year: date.getFullYear(),
//       // month: monthStr,
//       merchantId: 2655,
//       merchantInfoId: 2655
//     };
//     if (date.getMonth() === endDate.getMonth()) {
//       const month = date.getMonth() + 1;
//       const monthStr = month < 10 ? `0${month}` : `${month}`;
//       const calendarRes = await getCalendartickt({ ...params,  month: monthStr });
//       if (calendarRes.data.status === 200) {
//         const list = calendarRes.data.data;
//         return list.filter(item => dayjs(item.occDate) > dayjs(date) && dayjs(item.occDate) < dayjs(endDate));
//       }
//     } else {
//       const month = date.getMonth() + 1;
//       const monthStr = month < 10 ? `0${month}` : `${month}`;
//       const p1 = getCalendartickt({ ...params,  month: monthStr });

//       const month2 = date.getMonth() + 2;
//       const monthStr2 = month < 10 ? `0${month2}` : `${month2}`;
//       const p2 = getCalendartickt({ ...params,  month: monthStr2 });

//       const calendarRes = await Promise.all([p1, p2]);
//       let list: any[] = [];
//       if (calendarRes[0].data.status === 200) {
//         list = list.concat(calendarRes[0].data.data);
//       }

//       if (calendarRes[1].data.status === 200) {
//         list = list.concat(calendarRes[1].data.data);
//       }
//       return list.filter(item => dayjs(item.occDate) > dayjs(date) && dayjs(item.occDate) < dayjs(endDate));
//     }
//   }

//   return [];
// };

// const main = async () => {
//   const list = await getTicketDate();
//   console.log(JSON.stringify(list));
// }

main();